package com.xysq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.etsy.android.grid.StaggeredGridView;
import com.rockcent.action.CallbackListener;
import com.rockcent.model.BClientCustomerBO;
import com.rockcent.model.CustomerCouponBO;
import com.rockcent.model.constant.RealNameAuthType;
import com.umeng.analytics.MobclickAgent;
import com.xysq.adapter.WantBuyItemAdapter;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import java.util.List;

public class WantBuyActivity extends BaseActivity implements View.OnClickListener {

    private ProgressBar progressBar;
    private SwipeRefreshLayout swipeRefreshLayout;
    private StaggeredGridView staggeredGridView;
    private View headerView;
    private View footerView;
    private LinearLayout goToPublishBtn;
    private LinearLayout viewMyPublishedBtn;
    private WantBuyItemAdapter itemAdapter;
    private String loginName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_want_buy);
        initView();
    }

    private void initView() {
        headerView = LayoutInflater.from(this).inflate(R.layout.header_sgrid_want_buy, null);
        goToPublishBtn = (LinearLayout) headerView.findViewById(R.id.btn_go_to_publish);
        goToPublishBtn.setOnClickListener(this);
        viewMyPublishedBtn = (LinearLayout) headerView.findViewById(R.id.btn_view_my_published);
        viewMyPublishedBtn.setOnClickListener(this);

        footerView = LayoutInflater.from(this).inflate(R.layout.footer_load_more, null);

        staggeredGridView = (StaggeredGridView) findViewById(R.id.staggered_grid_view);
        staggeredGridView.addHeaderView(headerView);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        progressBar = (ProgressBar) findViewById(R.id.progress_loading);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestData();
            }
        });

        itemAdapter = new WantBuyItemAdapter(this, appAction);
        staggeredGridView.setAdapter(itemAdapter);
        staggeredGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CustomerCouponBO couponBO = (CustomerCouponBO) itemAdapter.getItem(position - 1);
                Intent intent = new Intent(getApplicationContext(), TipsActivity.class);
                intent.putExtra(TipsActivity.COUPONMODEL_ID, couponBO.getId());
                intent.putExtra(TipsActivity.CUSTOMER_ID, couponBO.getCustomerId());
                startActivity(intent);
            }
        });
    }

    private void requestData() {
        itemAdapter.clearItems();
        appAction.listRecommendCoupons(UserInfoKeeper.readCustomerId(this), new CallbackListener<List<CustomerCouponBO>>() {
            @Override
            public void onSuccess(List<CustomerCouponBO> data) {
                itemAdapter.addItems(data);
                progressBar.setVisibility(View.GONE);
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(getBaseContext(), message);
            }
        });
    }

    @Override
    public void onClick(View view) {
        view.setEnabled(false);
        loginName = UserInfoKeeper.readPhoneNumber(this);
        String customerId = UserInfoKeeper.readCustomerId(this);
        if (TextUtils.isEmpty(customerId)) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        } else {
            if (view == goToPublishBtn) {
                if (TextUtils.isEmpty(loginName)) {
                    startActivity(new Intent(WantBuyActivity.this, LoginActivity.class));
                } else {
                    checkIdentify();
                }
            }
            if (view == viewMyPublishedBtn) {
                Intent intent = new Intent(this, MyPublishedActivity.class);
                startActivity(intent);
            }
        }
    }

    //获取用户身份认证状态
    private void checkIdentify() {
        appAction.getBClientCustomer(loginName, new CallbackListener<BClientCustomerBO>() {
            @Override
            public void onSuccess(BClientCustomerBO data) {
                if (data.getIsRealnameAuth() == RealNameAuthType.UNAUTH) {//未实名认证，跳到实名认证页面
                    startActivity(new Intent(WantBuyActivity.this, IdentifyActivity.class));
                } else if (data.getIsRealnameAuth() == RealNameAuthType.FAIL) {
                    Intent intent = new Intent(WantBuyActivity.this, IdentifyActivity.class);
                    intent.putExtra(IdentifyActivity.FAIL, IdentifyActivity.FAIL);
                    startActivity(intent);
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTHING) {
                    ToastUtil.showShort(WantBuyActivity.this, getResources().getString(R.string.toast_authing));
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTH) {
                    Intent intent = new Intent(WantBuyActivity.this, PublishGoodsActivity.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        goToPublishBtn.setEnabled(true);
        viewMyPublishedBtn.setEnabled(true);

        requestData();
        MobclickAgent.onPageStart("我要页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("我要页");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == UserActivity.IDENTIFY_CODE) {
            boolean result = data.getBooleanExtra(UserActivity.RESULT, true);
            if (result) {//用户保存信息后返回
                startActivity(new Intent(WantBuyActivity.this, IdentifyActivity.class));
            } else {//用户直接返回，不做任何操作
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
